import { ipcRenderer, contextBridge } from 'electron'
import { Titlebar } from "tk-electron-titlebar";
// --------- Expose some API to the Renderer process ---------
contextBridge.exposeInMainWorld('ipcRenderer', {
  on(...args: Parameters<typeof ipcRenderer.on>) {
    const [channel, listener] = args
    return ipcRenderer.on(channel, (event, ...args) => listener(event, ...args))
  },
  off(...args: Parameters<typeof ipcRenderer.off>) {
    const [channel, ...omit] = args
    return ipcRenderer.off(channel, ...omit)
  },
  send(...args: Parameters<typeof ipcRenderer.send>) {
    const [channel, ...omit] = args
    return ipcRenderer.send(channel, ...omit)
  },
  invoke(...args: Parameters<typeof ipcRenderer.invoke>) {
    const [channel, ...omit] = args
    return ipcRenderer.invoke(channel, ...omit)
  },

  // You can expose other APTs you need here.
  // ...
})

window.addEventListener('DOMContentLoaded', () => {
  const params = new URLSearchParams(window.location.search);
  let fullscreen = true;
  if (params.has("fullscreen")) {
    fullscreen = params.get("fullscreen") === "true" ? true : false;
  }
  let icon = "./favicon.ico"
  if (window.location.href.includes('/extension')) {
    icon = `atom://${params.get("name")}/${params.get("version")}/dist/favicon.png`
  }
  new Titlebar({
    icon: icon,
    minimizable: true,
    maximizable: fullscreen,
    closeable: true,
    enableMnemonics: true,
    iconSize: 18,
    titleHorizontalAlignment: "left"
  });
});